package com.example.demo.service.impl;


import com.example.demo.entity.*;
import com.example.demo.mapper.IndustrialIndicatorsMapper;
import com.example.demo.service.IndustrialIndicatorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 工业指标
 */
@Service
public class IndustrialIndicatorsServiceImpl implements IndustrialIndicatorsService {
    @Autowired
    IndustrialIndicatorsMapper industrialIndicatorsMapper;

    /**
     * 分区镇规上工业总产值
     * @param year 年份
     * @param month 月份
     * @return List<SubDistrictTownIndustry>
     */
    @Override
    public List<SubDistrictTownIndustry> getSubDistrictTownIndustry(int year, int month){
        return industrialIndicatorsMapper.findSubDistrictTownIndustry(year, month);
    }

    /**
     * 工业总产值
     * 规上工业总产值
     * 综合能源消费量（吨标准煤）
     * 规上工业用电量（万千瓦时）
     * 万元产值能耗（吨标准煤/万元）
     * @param year 年份
     * @param indicators 指标
     * @return List<IndustrialEconomicIndicators>
     */
    @Override
    public List<IndustrialEconomicIndicators> getIndustrialIndicatorsByIndicators(int year, String indicators) {
        return industrialIndicatorsMapper.findIndustrialIndicatorsByIndicators(year, indicators);
    }

    /**
     * 高新技术产业产值
     * @param year 年份
     * @return List<HighTechIndustry>
     */
    @Override
    public List<HighTechIndustry> getHighTechIndustry(int year) {
        return industrialIndicatorsMapper.findHighTechIndustry(year);
    }

    /**
     * 头标：当月及当月累计
     * @param year 年份，month 月份, indicators 指标
     *@return IndustrialEconomicIndicators
     */
    @Override
    public IndustrialEconomicIndicators getMonthProduct(int year, int month, String indicators) {
        return industrialIndicatorsMapper.findMonthProduct(year, month, indicators);
    }

    /**
     * 规模以上工业分行业产值
     * @param year 年份，month 月份
     *@return List<BasicIndustry>
     */
    @Override
    public List<SectorProduct> getSectorProduct(int year, int month) {
        List<SectorProduct> list = industrialIndicatorsMapper.findSectorProduct(year, month);
        Iterator<SectorProduct> it = list.iterator();
        int i = 0;
        while (it.hasNext()) {
            SectorProduct sectorProduct = it.next();
            if(i == 0){
                sectorProduct.setSectorName("总计");
            }
            i++;
            sectorProduct.setNumber(i);
            if(sectorProduct.getPreviousAccumulate() == 0){
                continue;
            }else{
                double monthOnMonthAccumulate = (sectorProduct.getCurrentAccumulate() - sectorProduct.getPreviousAccumulate()) * 100 / sectorProduct.getPreviousAccumulate();
                double value = new BigDecimal(monthOnMonthAccumulate).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
                sectorProduct.setMonthOnMothAccumulate(value);
            }
        }
        return list;
    }


    /**
     * 重点工业企业产值完成情况（同一集团企业）
     * @param year 年份
     * @param month 月份
     * @return List<KeyEnterprisesProduct>
     */
    @Override
    public List<KeyEnterprisesProduct> getKeyEnterprisesProductByGroup(int year, int month) {
        List<KeyEnterprisesProduct> list = industrialIndicatorsMapper.findKeyEnterprisesProductByGroup(year, month);

        /**
         * 将KeyEnterprisesProduct添加到map<KeyEnterprisesProduct.group,MinKeyEnterprisesProduct>
         * MinKeyEnterprisesProduct用来统计相同group的总参数
         */
        Map<String, MinKeyEnterprisesProduct> map = new LinkedHashMap<String, MinKeyEnterprisesProduct>();
        for (KeyEnterprisesProduct kep : list) {
            MinKeyEnterprisesProduct mKep;
            if(map.containsKey(kep.getGroup())){
                mKep = map.get(kep.getGroup());
            }else{
                mKep = new MinKeyEnterprisesProduct();
            }
            mKep.add(kep);
            map.put(kep.getGroup(), mKep);
        }

        /**
         * 遍历map，将总group插入到list集合
         */
        Integer i = 0;
        int number = 1;
        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        while(it.hasNext()){
            String key = it.next();
            MinKeyEnterprisesProduct mKep = map.get(key);
            KeyEnterprisesProduct kep = new KeyEnterprisesProduct(number,key,mKep);
            list.add(i,kep);
            i = i + mKep.getCount() + 1;
            number++;
        }
        return list;
    }


    /**
     * 重点工业企业产值完成情况（同一法人企业）
     * @param year 年份
     * @param month 月份
     * @return List<KeyEnterprisesProduct>
     */
    @Override
    public List<KeyEnterprisesProduct> getKeyEnterprisesProductByJudicialPerson(int year, int month) {
        List<KeyEnterprisesProduct> list = industrialIndicatorsMapper.findKeyEnterprisesProductByJudicialPerson(year, month);

        /**
         * 将KeyEnterprisesProduct添加到map<KeyEnterprisesProduct.judicialPerson,MinKeyEnterprisesProduct>
         * MinKeyEnterprisesProduct用来统计相同judicialPerson的总参数
         */
        Map<String, MinKeyEnterprisesProduct> map = new LinkedHashMap<String, MinKeyEnterprisesProduct>();
        for (KeyEnterprisesProduct kep : list) {
            MinKeyEnterprisesProduct mKep;
            if(map.containsKey(kep.getJudicialPerson())){
                mKep = map.get(kep.getJudicialPerson());
            }else{
                mKep = new MinKeyEnterprisesProduct();
            }
            mKep.add(kep);
            map.put(kep.getJudicialPerson(), mKep);
        }

        /**
         * 遍历map，将总judicialPerson插入到list集合
         */
        Integer i = 0;
        int number = 1;
        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        while(it.hasNext()){
            String key = it.next();
            MinKeyEnterprisesProduct mKep = map.get(key);
            KeyEnterprisesProduct kep = new KeyEnterprisesProduct(number,key,mKep);
            list.add(i,kep);
            i = i + mKep.getCount() + 1;
            number++;
        }
        return list;
    }

    /**
     * 重点工业企业产值完成情况（单独企业）
     * @param year 年份
     * @param month 月份
     * @return List<KeyEnterprisesProduct>
     */
    @Override
    public List<KeyEnterprisesProduct> getKeyEnterprisesProductAlone(int year, int month) {
        List<KeyEnterprisesProduct> list = industrialIndicatorsMapper.findKeyEnterprisesProductAlone(year, month);
        int number = 1;
        Iterator<KeyEnterprisesProduct> it = list.iterator();
        while(it.hasNext()){
            KeyEnterprisesProduct kep = it.next();
            kep.setNumber(number);
            number++;
        }
        return list;
    }
}
